package newx.com.control.scroll.text 
{
	import engine.scene.SceneManager;
	import engine.tip.TipsMainLogic;
	import engine.ui.FaceIconViewUI;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	
	import newx.NxSprite;
	import newx.com.control.text.NxLabel;
	
	/**
	 * ...
	 * @author wanghb
	 */
	public class NxTalkTextFiled extends NxSprite 
	{
		//显示文本
		private var txt:TextField;
		//表情容器
		private var iconSprite:Sprite;
		//文本的宽度
		private var _textWidth:Number;
		private var _textColor:uint;
		//表情字符数组
		private var _faceIconArray:Array;
		//表情索引数组
		private var _faceIndexArray:Array;
		//表情动画是否停止了
		private var _isStopFactIcon:Boolean;
		
		private var _iconWidth:int	=	32;
		private var _iconHeight:int	=	32;
		private var isLink:Boolean		=	false;
		public function NxTalkTextFiled(width_:Number) 
		{
			_textWidth		=	width_;
			iconSprite		=	new Sprite();
			txt				=	new TextField();
			txt .selectable	=	false;
			txt.autoSize	=	TextFieldAutoSize.LEFT;
			txt.multiline	=	true;
			txt.wordWrap	=	true;
			txt.selectable	=	false;
			this.addChild(txt);
			this.addChild(iconSprite);
			txt.addEventListener(TextEvent.LINK, textLinkHandle);
			txt.addEventListener(MouseEvent.CLICK, textClickHandle);
			txt.addEventListener(MouseEvent.MOUSE_MOVE, function(e:MouseEvent):void{if(TipsMainLogic.instance.isMainShow) TipsMainLogic.instance.hide();});
		}
		/**
		 * 设定当前说话的内容.
		 * @param	val.
		 */
		public function insterTalkStr(val:String):void
		{
			txt.width			=	_textWidth;
			txt.htmlText		=	checkTalkStr(val);
			txt.autoSize		=	TextFieldAutoSize.LEFT;
			drawFaceIcon();
			txt.filters = [ NxLabel.glow ];
		}
		/**
		 * 设定文本宽度
		 */
		public function set textWidth(value:Number):void 
		{
			_textWidth 		=	value;
			txt.width		=	_textWidth;
			txt.autoSize	=	TextFieldAutoSize.LEFT;
		}
		public function get showTextWidth():Number
		{
			return txt.textWidth;
		}
		public function get showTextHeight():Number
		{
			return txt.textHeight;
		}
		public function get isStopFactIcon():Boolean 
		{
			return _isStopFactIcon;
		}
		
		public function set isStopFactIcon(value:Boolean):void 
		{
			_isStopFactIcon = value;
		}
		
		public function set textColor(value:uint):void 
		{
			_textColor = value;
			txt.textColor	=	_textColor;
		}
		/**
		 * 清空文本
		 */
		public function clearAll():void
		{
			clearIconSprite();
			txt.htmlText	=	"";
		}
		/**
		 * 链接事件
		 * @param	e
		 */
		private function textLinkHandle(e:TextEvent):void
		{
			var tFunc:Function;
			if(data.CheckProperties("onLinkHandle"))
			{
				isLink	=	true;
				tFunc	=	data.GetProperties("onLinkHandle") as Function; 
				tFunc.call(this,e.text);
				tFunc	=	null;
			}
		}
		
		private function textClickHandle(e:MouseEvent):void
		{
			if(!isLink)
			{
				SceneManager.getInstance().norSceneView.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_DOWN));
			}
			isLink	=	false;
			SceneManager.getInstance().norSceneView.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_UP));
		}
		
		
		/**
		 * 绘制表情图标.
		 */
		private function drawFaceIcon():void
		{
			clearIconSprite();
			_faceIndexArray	=	[];
			var talkStr:String	=	txt.text;
			var strLength:int	=	talkStr.length;
			//获取表情插入索引.
			for (var i:int = 0; i < strLength; i++) 
			{
				if (talkStr.charAt(i) == "　")
				{
					_faceIndexArray.push(i);
				}
			}
			
			txt.height	=	txt.height;
			txt.width	=	txt.width;
			
			//贴表情.
			for (var j:int = 0; j < _faceIndexArray.length; j++) 
			{
				
				var iconRect:Rectangle	=	txt.getCharBoundaries(_faceIndexArray[j]);
				var iconUrl:String		=	"bq" + (_faceIconArray[j] as String).substr(1, (_faceIconArray[j] as String).length);
				var icon:MovieClip		=	FaceIconViewUI.getInstance().borrowFaceIcon(iconUrl);
				if (iconRect != null && icon != null) 
				{
					icon["url"]	=	iconUrl;
					icon.x		=	iconRect.x	+ (iconRect.width - _iconWidth) * .5	+	16;
					icon.y		=	iconRect.y	+ (iconRect.height - _iconHeight)		+	16;
					iconSprite.addChild(icon);
				}
			}
			_isStopFactIcon	=	false;
		}
		/**
		 * 清空表情.
		 */
		private function clearIconSprite():void
		{
			while (iconSprite.numChildren > 0) 
			{
				var clearIcon:MovieClip	=	iconSprite.getChildAt(0) as MovieClip;
				FaceIconViewUI.getInstance().returnFaceIcon(clearIcon["url"], clearIcon);
				iconSprite.removeChild(clearIcon);
			}
		}
		/**
		 * 停止所有表情动画
		 */
		public function stopAllFaceIcon():void
		{
			for (var i:int = 0; i < iconSprite.numChildren; i++) 
			{
				var stopObj:MovieClip	=	iconSprite.getChildAt(i) as MovieClip;
				stopObj.gotoAndStop(1);
			}
			_isStopFactIcon	=	true;
		}
		/**
		 * 过虑掉全角空格，提取表情图标的字符串.
		 * 将表情字符替换为32像素的全角空格.
		 * @param	val.
		 * @return
		 */
		private function checkTalkStr(val:String):String
		{
			val				=	val.replace(/\　/g, " ");
			_faceIconArray	=	[];
			_faceIconArray	=	val.match(/\*([0-1][0-9])/g);
			val = val.replace(/\*([0-1][0-9])/g, "<font size='32'>　</font>");
			return val;
		}

	}

}